package com.fahai.web.service.impl;


import com.fahai.common.PageVo;
import com.fahai.web.dao.CaseInvstgDao;
import com.fahai.web.entity.CaseInvstg;
import com.fahai.web.entity.UserInfo;
import com.fahai.web.entity.UserLawyer;
import com.fahai.web.service.CaseInvstgService;
import com.fahai.web.service.UserInfoService;
import com.fahai.web.service.UserLawyerService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.gson.JsonObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;


/**
 * Created by jiangchong on 2017/5/17.
 */
@Service
public class CaseInvstgServiceImpl implements CaseInvstgService {

    private static final Logger logger = LoggerFactory.getLogger(CaseInvstgService.class);
    private static AtomicInteger lawyerIdAtomic = new AtomicInteger(1);
    @Autowired
    CaseInvstgDao caseInvstgDao;
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private UserLawyerService userLawyerService;
    @Override
    public List<CaseInvstg> queryCaseInvstg(CaseInvstg caseInvstg, PageVo pageVo)
    {
        Page<CaseInvstg> page = PageHelper.startPage(pageVo.getPageNum(), pageVo.getPageSize());
        return caseInvstgDao.getcaseInvstgList(caseInvstg );
    }
   @Override
    public List<CaseInvstg> queryCaseInvstg(CaseInvstg caseInvstg )
    {
        return caseInvstgDao.getcaseInvstgList(caseInvstg  );
    }

    @Override
    public int countCaseInvstg(CaseInvstg caseInvstg) {
        return caseInvstgDao.getcount(caseInvstg);
    }


    /**
     * 更新发布等级与备注
     *
     * @param caseInvstg
     */
    @Override
    public void update(CaseInvstg caseInvstg) {
        CaseInvstg vo= caseInvstgDao.selectByPrimaryKey(caseInvstg.getId());
        //已经发布的数据不更改状态
        if(vo.getBuystatus()==0){
            caseInvstgDao.updatelevel_fb(caseInvstg);

            // todo 插入新的用户以及律师的关联关系
            logger.info("--> 将创建基于案例的用户............................");
            if (caseInvstg.getLevel() != 4) {
                createUser(vo);
            }
        }else{
            caseInvstgDao.updatelevel_fbed(caseInvstg);
        }
    }

    private synchronized void createUser(CaseInvstg vo) {
        // 查询该用户是否已经添加到平台
        List<UserInfo> userInfoList = userInfoService.queryUserInfo(7, vo.getUphone());

        // 如果没有则添加用户信息
        if (userInfoList.isEmpty()) {
            // 1。获取用户名、用户手机号码、手机号码地区
            UserInfo userInfo = new UserInfo();
            userInfo.setProid(7);
            userInfo.setUname(vo.getUname());
            userInfo.setUphone(vo.getUphone());
            userInfo.setUphoneArea(vo.getUphonearea());
            userInfo.setUtype(0);
            userInfo.setUrmoney(0);
            userInfo.setIsconf(1);
            userInfo.setUstates(1);
            userInfo.setIsvalid(1);
            userInfo.setAtimes(0);
            userInfo.setBak("来自案例");
            userInfoService.insertUserInfo(userInfo);
            logger.info("--> 开始创建基于案例的用户............................" + userInfo.toString());

            int mod = lawyerIdAtomic.incrementAndGet() % 3;
            // 添加与律师的关联关系
            UserLawyer userLawyer = new UserLawyer();
            userLawyer.setLawyerid(mod == 0 ? 12 : mod == 1 ? 1 : 4);
            userLawyer.setUserid(userInfo.getUserid());
            userLawyer.setLaConTag(0);
            userLawyer.setUsConTag(1);
            userLawyer.setCallTime(0);
            userLawyer.setBak("来自案例");
            userLawyerService.insertUserLawyer(userLawyer);
            logger.info("--> 开始创建基于案例的律师与用户的关联关系............................" + userLawyer.toString());
        }
        logger.info("--> 结束创建基于案例的用户............................");
    }
}
